<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>POS机管理</title>
		<link rel="stylesheet" href="__PEAR__/component/pear/css/pear.css" />
		<style>
		.layui-input{width:130px;}
		.layui-form-label{width:auto;}
		.layui-form-item .layui-input-inline{width: 130px;}
		</style>
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">POS机编号</label>
							<div class="layui-input-inline">
								<input type="text" name="posid" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">门店编号</label>
							<div class="layui-input-inline">
								<input type="text" name="branchno" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="pos-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md">
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="pos-table" lay-filter="pos-table"></table>
			</div>
		</div>

		<script type="text/html" id="pos-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        新增
		    </button>
		</script>

		<script type="text/html" id="pos-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>

		<script type="text/html" id="pos-enable">
			<input type="checkbox" name="enable" value="{{d.posid}}|{{d.branch_no}}" lay-skin="switch" lay-text="正常|停用" lay-filter="pos-enable" {{ d.status == 1 ? 'checked' : '' }}>
		</script>
		
		<script type="text/html" id="pos-bind">
			<input type="checkbox" name="bind" value="{{d.posid}}|{{d.branch_no}}" lay-skin="switch" lay-text="已绑定|已解绑" lay-filter="pos-bind" {{ d.load_flag == 1 ? 'checked' : '' }} {{ d.load_flag == 0 ? 'disabled' : '' }}>
		</script>

		<script type="text/html" id="pos-createTime">
			{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
		</script>

		<script src="__STATIC__/lib/layui/layui.js"></script>
		<script src="__PEAR__/component/pear/pear.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery','common'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;

				let MODULE_PATH = "/{$MODULE_NAME}/pos/posno/";

				let cols = [
					[
						{
							title: '行号',
							field: 'rowIndex',
							align: 'center',
							width: 80
						},
						{
							title: 'POS机编号',
							field: 'posid',
							align: 'center',
							width: 100
						},
						{
							title: '门店编号',
							field: 'branch_no',
							align: 'center'
						},
						{
							title: '计算机名',
							field: 'hostname',
							align: 'center'
						},
						{
							title: '绑定状态',
							field: 'load_flag',
							align: 'center',
							templet: '#pos-bind',
						},
						{
							title: 'POS机状态',
							field: 'status',
							align: 'center',
							templet: '#pos-enable',
						},
						{
							title: '联系人',
							field: 'branch_man',
							align: 'center'
						},
						{
							title: '最后登录时间',
							field: 'lasttime',
							align: 'center'
						},
						{
							title: '最后操作员',
							field: 'lastcashier',
							align: 'center'
						},
						{
							title: 'POS机说明',
							field: 'posdesc',
							align: 'center'
						},
						{
							title: '操作',
							toolbar: '#pos-bar',
							align: 'center',
							width: 130
						}
					]
				]

				table.render({
					elem: '#pos-table',
					url: MODULE_PATH+"dataList",
					page: true,
					cols: cols,
					skin: 'line',
					toolbar: '#pos-toolbar',
					defaultToolbar: []
				});

				table.on('tool(pos-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(pos-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					}
				});

				form.on('submit(pos-query)', function(data) {
					table.reload('pos-table', {
						where: data.field,
						page:{curr:1}
					})
					return false;
				});

				form.on('switch(pos-enable)', function(obj) {
					
					var arr=this.value.split("|");
				   	var posid=arr[0];
				    var branch_no=arr[1];
				    
				    $.ajax({
							url:MODULE_PATH+"stop",
							data:{"posid":posid,'branch_no':branch_no},
							dataType: 'json',
							type: 'post',
							success: function(result) {
								//如果不为空，则填充数据
								layer.msg(result.msg);
							}
						})
				});
				
				form.on('switch(pos-bind)', function(obj) {

					var arr=this.value.split("|");
				   	var posid=arr[0];
				    var branch_no=arr[1];
				    
				    $.ajax({
							url:MODULE_PATH+"unbind",
							data:{"posid":posid,'branch_no':branch_no},
							dataType: 'json',
							type: 'post',
							success: function(result) {
								//如果不为空，则填充数据
								table.reload('pos-table', {})
							}
						})
				});

				window.add = function() {
					layer.open({
						type: 2,
						title: '新增POS机',
						shade: 0.1,
						area: [common.isModile()?'100%':'450px', common.isModile()?'100%':'300px'],
						content: MODULE_PATH + 'add.html'
					});
				}

				window.edit = function(obj) {
					var _id=obj.data.posid;
					layer.open({
						type: 2,
						title: '修改POS机',
						shade: 0.1,
						area: ['450px', '300px'],
						content: MODULE_PATH + 'edit/posid/'+_id+'.html'
					});
				}

				window.remove = function(obj) {
					layer.confirm('确定要删除该POS机?', {
						icon: 3,
						title: '删除提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "deletePos/posid/" + obj.data['posid'],
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.code) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										obj.del();
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				window.refresh = function(param) {
					table.reload('pos-table');
				}
			})
		</script>
	</body>
</html>
